package cn.iocoder.yudao.module.product.evaluation.api;

import cn.iocoder.yudao.framework.business.evaluation.enums.ScoreTypeEnum;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.api.product.evaluation.ProductEvaluationApi;
import cn.iocoder.yudao.module.api.product.evaluation.dto.ProductEvaluationCreateReqDTO;
import cn.iocoder.yudao.module.api.product.evaluation.dto.ProductEvaluationRespDTO;
import cn.iocoder.yudao.module.product.evaluation.dal.dataobject.ProductEvaluationDO;
import cn.iocoder.yudao.module.product.evaluation.service.ProductEvaluationService;
import cn.iocoder.yudao.module.product.evaluation.vo.AppProductEvaluationCreateReqVO;
import com.google.common.collect.Maps;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.util.Map;
import java.util.Set;

/**
 * 产品评价 API 实现类
 *
 * @author HUIHUI
 */
@Service
@Validated
public class ProductEvaluationApiImpl implements ProductEvaluationApi {

    @Resource
    private ProductEvaluationService productEvaluationService;

    @Override
    public Long createComment(Long userId, ProductEvaluationCreateReqDTO dto) {
        AppProductEvaluationCreateReqVO reqVO = BeanUtils.toBean(dto, AppProductEvaluationCreateReqVO.class);
        return productEvaluationService.createComment(userId, reqVO);
    }

    @Override
    public float calculateScoreByShopId(Long shopId) {
        return productEvaluationService.calculateScoreByShopId(shopId);
    }

    @Override
    public Map<Long, Float> batchCalculateScoreByShopIds(Set<Long> shopIds) {
        return productEvaluationService.batchCalculateScoreByShopIds(shopIds);
    }

    @Override
    public PageResult<ProductEvaluationRespDTO> getMyCommentPage(Long userId, PageParam pageVO) {
        PageResult<ProductEvaluationDO> result = productEvaluationService.getMyCommentPage(userId, pageVO);
        return BeanUtils.toBean(result, ProductEvaluationRespDTO.class);
    }

    @Override
    public Map<String, Integer> getCommentCount(Long shopId) {
        Map<String, Integer> countMap = Maps.newLinkedHashMapWithExpectedSize(6);
        // 全部
        countMap.put("allCount", productEvaluationService.getCount(shopId, ScoreTypeEnum.ALL, null));
        // 好评
        countMap.put("positiveCount", productEvaluationService.getCount(shopId, ScoreTypeEnum.POSITIVE, null));
        // 差评
        countMap.put("negativeCount", productEvaluationService.getCount(shopId, ScoreTypeEnum.NEGATIVE, null));
        // 有图
        countMap.put("hasPictureCount", productEvaluationService.getCount(shopId, ScoreTypeEnum.ALL, true));
        // 好评+有图
        countMap.put("positiveHasPictureCount", productEvaluationService.getCount(shopId, ScoreTypeEnum.POSITIVE, true));
        // 差评+有图
        countMap.put("negativeHasPictureCount", productEvaluationService.getCount(shopId, ScoreTypeEnum.NEGATIVE, true));
        return countMap;
    }

    @Override
    public Map<Long, Integer> getCommentCountMapByShopIds(Set<Long> shopIds) {
        return productEvaluationService.getCommentCountByShopIds(shopIds);
    }

}
